import { action, observable } from "mobx";
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import _ from 'lodash';


export class ReplyAddStore {
    @observable visible: boolean = false;
    @observable loading: boolean = false;
    @observable contentid: string = '';
    @observable content: string = '&#8203;';
    @observable optdesc: string = '';

    @observable maincategory: string = '';
    @observable subcategory: string = '';
    @observable seccategory = '';  
    @observable replyShowType = 'new';

    @observable   popup: boolean = false;
    @observable isInitPopup: boolean = false;
    @observable popupOffset = {
        left: 0, top: 0
    }
    @observable popupArrowPostion: boolean = false;
    @observable atInfo:any = {};
    @observable editor:any;
    @observable loadJSAndInitAt:any

    @observable isdefault: boolean = true;
    @observable showbuttons:any = {};

    src: string = 'submit';
    billid: string = '';
    type: string = '1';
    iscreate: string = '1';
    isEditOpt: string = '0';
    temp_Quotesid: string = '';
    temp_CommentTopid: string = '';
    temp_Commentid: string = '';
    temp_CommentUsersid: string = '';
    okClickType = '';//当为评论和引用时，点击提交需要把滚动条滚动到最后,点击按钮时才记录类型，okClickType=replyShowType='new' 或者'quote'
    callSubmit: (params: any) => void = () => { };



    initReplyAddStore = (props) => {
        this.update(props)
    }

    @action
    getAtInfo(params){
		let key = "cube_at_"+params.modeId+"_"+params.billid;
		let lasttime:any = this.atInfo[key+"_time"];
		let data:any = this.atInfo[key+"_data"];
		return {time:lasttime?lasttime:'',data:data?data:{}};
	}

    @action
	setAtInfo=(k,v,params)=>{
		let key = "cube_at_"+params.modeId+"_"+params.billid;
		this.atInfo[key+"_"+k] = v;
    }
    

    @action
    onCancel = () => {
        this.visible = false;
        // this.content = '&#8203;';
    }
    @action
    onPopupCancel = () => {
        this.popup = false;
    }
    @action
    onOk = () => {
        this.okClickType = this.replyShowType;//点击时记录提交的类型
        this.loading = true;
        this.callSubmit({
            iscreate: this.iscreate,
            type: this.type,
            replycontent: this.content,
            billid: this.billid,
            src: this.src,
            temp_Quotesid: this.temp_Quotesid,
            temp_CommentTopid: this.temp_CommentTopid,
            temp_Commentid: this.temp_Commentid,
            temp_CommentUsersid: this.temp_CommentUsersid,
            isEditOpt: this.isEditOpt
        })
    }

    @action
    onChange = (content: string) => {
        this.content = content;
    }

    @action
    update = (param: any) => {
        for (let key in param) {
            _.set(this, key, _.get(param, key));
        }
    }
    @action
    clearContent=()=>{
        this.initContent({}, 'new');
    };
    @action
    setEditor = (editor,initAt?)=> {
        this.loadJSAndInitAt = initAt;
        this.editor = editor;
    }

    @action
    initContent = (replyData: any, replyShowType: string) => {
        let content:any = _.get(replyData, 'replycontent');
        let opt = "";
        if(replyShowType=='edit'){
            opt = getLabel(93, '编辑');
        }else if(replyShowType=='quote'){
            opt = getLabel(19422, '引用');
            content = "<p></p>";
        } else if(replyShowType=='comment'){
            opt = getLabel(675, '评论');
            content = "<p></p>";
        } else if(replyShowType == "new"){
            content = "&#8203;";
        }
        if(opt!=""){
            this.optdesc = "<p class='messageTitle' style='color: #999999;font-size: 9px;'>"+opt+" [ #"+replyData.floornum+" "+replyData.replyorname+" ] "+getLabel('33800',"的")+getLabel('33368',"内容")+":<span style='display:none'>#@split@#</span></p>"
        }else{
            this.optdesc = "";
        }
        
        if (replyShowType) {
            this.replyShowType = replyShowType;
            this.content = content;
            if(this.editor){
                this.editor.setData(this.content,{callback:()=> {
                    this.loadJSAndInitAt();
                }})
            }
            this.temp_Commentid = '';
            this.temp_CommentTopid = '';
            this.temp_CommentUsersid = '';
            this.temp_Quotesid = '';
            this.type = '';
            this.iscreate = '';
            if (replyShowType == "new") {
                this.type = '1';
                this.iscreate = '1';
                this.billid = '';
                this.src = 'submit';
                this.isEditOpt = '0';
                this.temp_Commentid = '';
                this.temp_CommentTopid = '';
                this.temp_CommentUsersid = '';
            } else if (replyShowType == "edit") {
                this.type = '2';
                this.iscreate = '0';
                this.billid = _.get(replyData, 'id');
                this.src = 'edit';
                this.isEditOpt = '1';
            } else if (replyShowType == "quote") {
                this.type = '1';
                this.iscreate = '1';
                this.billid = _.get(replyData, 'id');
                this.src = 'quote';
                this.isEditOpt = '0';
                this.temp_Quotesid = _.get(replyData, 'id');
                this.content = content;
            } else if (replyShowType == 'comment') {
                this.type = '1';
                this.iscreate = '1';
                this.billid = _.get(replyData, 'id');
                this.src = 'comment';
                this.content = content;
                this.temp_Commentid = _.get(replyData, 'id');
                this.temp_CommentTopid = _.get(replyData, 'id');
                this.temp_CommentUsersid = _.get(replyData, 'replyor');
                this.isEditOpt = '0';
            } else {
                this.type = '1';
                this.iscreate = '0';
                this.billid = '';
                this.src = '';
                this.isEditOpt = '0';
            }
        }
    }
}